<?php

namespace app\common\model;

use think\Model;

class Article extends Model
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'add_at';
    protected $updateTime = 'update_at';
    public function article_list($where='',$page=1,$size=10)
    {
        $fields="c.c_name,a.id,title,content,author,click,thum_img,add_at,update_at";
        $where['a.status'] = 1;
        $list = $this->alias('a')->field($fields)->where($where)->join('yu_category c',"a.c_id = c.id")->order('a.id desc')->paginate($size,true,['type'=>'bootstrap','page'=>$page]);
        foreach($list as $k=>$v){
            $list[$k]['remark']= clear_md($v['content']);
        }
        $data['page'] = $list->render();
        $data['list'] = $list;
        return $data;
    }

    public function _list($page=1,$size=10){
        $fields="c.c_name,a.id,title,content,author,click,thum_img,add_at,update_at";
        $list = $this->alias('a')->field($fields)->where('a.status',1)->join('yu_category c',"a.c_id = c.id")->order('a.id desc')->paginate($size,true,['type'=>'bootstrap','page'=>$page]);
        foreach($list as $k=>$v){
            $list[$k]['remark']= clear_md($v['content']);
        }
        $data['page'] = $list->render();
        $data['list'] = $list;
        return $data;
    }

    public function _save($data){
        if(intval($data['id'] > 0)){
            $data['update_at']=time();
            return  $this->where('id',$data['id'])->update($data);
        }else{
            $data['add_at'] = $data['update_at']=time();
            return  $this->insert($data);
        }
    }

}
